//线路树模块
define('lineTree', ['zTree'], function () {

    var setting = {
        view: {
            showIcon: true,
            selectedMulti: false
        },
        data: {
            key: {
                name: "name"
            },
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: 0
            }
        },
        callback: {
            beforeClick: function (treeId, treeNode) {
                return treeNode.type == 'LINE';
            },
            onClick: function (event, treeId, treeNode) {
                if (treeNode.id == lineTree.lineId) {
                    return;
                }
                var loadMask = $.createMask();
                setLine(treeNode);

                lineTree.nodeClick(lineTree.lineId, lineTree.dateType, treeNode);
                setTimeout(function () {
                    loadMask.remove();
                }, 50);
            }
        }
    };

    var cookieLineId = $.getLineCookie(),
        cookieDateType = $.getDateTypeCookie();


    /**
     * 设置当前线路名称及ID
     * @param item ztree节点
     */
    function setLine(item) {
        lineTree.lineId = item.id;
        $("#showlineName").html("当前线路 : " + item.name);
        lineTree.lineName = item.name;
    }

    //加载完成树之后自动选中一个line节点并触发点击事件
    function autoClick(item) {
        setLine(item);
        lineTree.nodeClick(lineTree.lineId, lineTree.dateType, item);
    }

    /**
     * 生成线路树
     */
    function initLineTree(loadFn) {
        lineTree.nodeClick = loadFn;
        $.ajaxExt({
            loadMask: true,
            url: "/baseOrgan/getLeftOrg4Line.htm"
        }).done(function (result) {
            if ($.isArray(result)) {
                var firstLineNode;//第一个取得的Line节点
                $.each(result, function (index, item) {

                    if (cookieLineId) {//有可能保存的cookie LineId在getLeftOrg4Line返回的数据中没有，所以需要特殊处理下
                        if (!firstLineNode && item.type == 'LINE') {
                            firstLineNode = item;
                        }
                        if (cookieLineId == item.id) {
                            autoClick(item);
                            firstLineNode = null;
                            return false;
                        }
                    } else {
                        if (item.type == 'LINE') {
                            autoClick(item);
                            return false;
                        }
                    }
                });

                if (firstLineNode) {//如果循环结束后还有值说明没有找到匹配的节点
                    autoClick(firstLineNode);
                }
                $("#lineTree").createTree(setting, result, {search: $('#lineSelected')}).expandAll(true);
            }
        });


        $('#dateDiv').on('click', 'a', function () {
            if (!$(this).hasClass('active')) {
                lineTree.dateType = $(this).attr('val');
                $(this).addActiveClass();
                $.setDateType(lineTree.dateType);
                lineTree.nodeClick(lineTree.lineId, lineTree.dateType);
            }
        });
    }

    var lineTree = {
        init: initLineTree,
        lineId: null,
        dateType: '02',
        lineName: '',
        nodeClick: $.noop
    };


    if (cookieDateType) {
        $('#dateDiv').find('a[val=' + cookieDateType + ']').addActiveClass();
        lineTree.dateType = cookieDateType;
    }

    return lineTree;
});

//区域不良行为统计
define('model1', function () {
    var currentLineId, currentDateType;

    var colors = ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3'];

    var keyColor = [];//用于保存图例的名称及对应的颜色以使得饼图和柱状图中间的每个图例颜色需要保持一致
    function createBarChart(id, data) {

        var myChart = echarts.init(document.getElementById(id));
        if (data) {

            var yLabel = [];

            var series = [];
            var i = 0;
            $.each(badBhvInfo, function (key, value) {
                series.push({
                    name: value,
                    type: 'bar',
                    stack: "数量",
                    barMaxWidth: '20',
                    itemStyle: {
                        normal: {
                            color: colors[i]
                        }
                    },
                    data: (function () {
                        var arr = [];
                        $.each(data, function (i, item) {
                            arr.push(item[key]);
                        });
                        return arr;
                    })()
                });
                i++;
            });

            $.each(data, function (i, item) {
                yLabel.push(item.areaName);
            });


            var option = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                grid: {
                    top: 0,
                    left: '3%',
                    right: '4%',
                    bottom: 0,
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    "axisLine": {
                        lineStyle: {
                            color: '#fff'
                        }
                    }
                },
                yAxis: {
                    type: 'category',
                    data: yLabel,
                    "axisLine": {
                        lineStyle: {
                            color: '#fff'
                        }
                    }
                },
                series: series
            };
            myChart.setOption(option);
        } else {
            myChart.clear();
        }
    }

    var badBhvInfo = null,//图例信息
        $legend = $('#chart-legend');

    function createLegend(data) {
        var html = '';
        var i = 0;
        badBhvInfo = data;

        $.each(data, function (key, value) {
            html += '<span style="margin-left: 10px;">' +
            '<span class="legend-label" style="background-color: ' + colors[i] + ';">&nbsp;</span>' +
            '<span class="legend-label-text">' + value + '</span>' +
            '</span>';
            i++;
            keyColor.push({
                name: key,
                value: value,
                color: colors[i]
            });
        });
        $legend.html(html);
    }

    //加载横向柱状图数据
    function loadBarData(dateType, lineId) {
        $.ajaxExt({
            url: '/lineDrvStandard/getLineAreaBadBhvSortList.htm',
            data: {
                lineId: lineId,
                dateTy: dateType
            },
            success: function (data) {
                data = data || {};

                loadPieData(currentDateType, currentLineId);//因为这个中的方法有用到loadBarData中方法的badBhvInfo这个信息
                $('#info1,#info2,#info3,#info4').empty();

                if (data.timeInterval.beginTime && data.timeInterval.endTime) {
                    $('#title1').html('区域不良行为统计({0}~{1})'.format(data.timeInterval.beginTime, data.timeInterval.endTime));
                } else {
                    $('#title1').html('区域不良行为统计');
                }
                createLegend(data.badBhvInfo);
                createBarChart('chart1', data.blackSpot);
                createBarChart('chart2', data.site);
                createBarChart('chart3', data.siteInterval);
                createBarChart('chart4', data.noOpt);

                data.lineAreaBadBhvInfo && $.each(data.lineAreaBadBhvInfo, function (i, item) {
                    var html = '总计：{0}次 <span class="{1}">{2} {3}%</span>'.format(item.sumAreaTypeFreq, item.sumAreaTypeFreqGth ? 'up' : 'down', item.sumAreaTypeFreqGth ? '↑' : '↓', item.sumAreaTypeFreqGth);
                    var id = '';
                    switch (item.areaType) {
                        case 'B':
                            id = 'info1';
                            break;
                        case 'S':
                            id = 'info2';
                            break;
                        case 'BS':
                            id = 'info3';
                            break;
                        case 'OL':
                            id = 'info4';
                            break;
                    }
                    $('#' + id).html(html);
                });
            }
        })
    }

    function createPieChart(id, data) {

        var myChart = echarts.init(document.getElementById(id));
        if (data) {

            var seriesData = [];
            $.each(keyColor, function (i, item) {
                seriesData.push({
                    value: data[item.name] || [],
                    name: item.value,
                    color: item.color || ''
                })
            });
            var option = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: {d} %,{c}次'
                },
                series: [
                    {
                        name: '当期占比',
                        type: 'pie',
                        radius: '40%',
                        label: {
                            normal: {
                                formatter: '{b}: {d} %,{c}次'
                            }
                        },
                        data: seriesData
                    }
                ]
            };
            myChart.setOption(option);
        }
    }

    //加载饼图数据
    function loadPieData(dateType, lineId) {
        keyColor = [];
        $.ajaxExt({
            url: '/lineDrvStandard/getLineAreaBadBhvCntList.htm',
            data: {
                lineId: lineId,
                dateTy: dateType
            },
            success: function (data) {
                data = data || [];
                $('#chart5,#chart6,#chart7,#chart8').empty();
                $.each(data, function (i, item) {
                    switch (item.areaType) {
                        case 'B':
                            createPieChart('chart5', item);
                            break;
                        case 'S':
                            createPieChart('chart6', item);
                            break;
                        case 'BS':
                            createPieChart('chart7', item);
                            break;
                        case 'OL':
                            createPieChart('chart8', item);
                            break;
                    }
                });
            }
        })
    }


    function createLineChart(data) {
        var myChart = echarts.init(document.getElementById('chart9'));
        if (data) {

            var xAxis = [],
                series = [],
                legendData = [];

            var timeHtml = '';
            $.each(data, function (key, value) {
                if (key == 'timeInterval') {
                    timeHtml = '时间范围（{0}-{1}）'.format(value.beginTime, value.endTime);
                    return;
                }

                if (!xAxis.length && !$.isEmptyObject(value)) {
                    $.each(value, function (key1, value1) {
                        xAxis.push(key1);
                    });
                }
                var name = '', seriesData = [];
                switch (key) {
                    case 'site':
                        name = '站点区域';
                        break;
                    case 'siteInterval':
                        name = '站点间区域';
                        break;
                    case 'blackSpot':
                        name = '黑点区域';
                        break;
                    case 'noOpt':
                        name = '非营运';
                        break;
                    case 'totalCnt':
                        name = '汇总';
                        break;
                }

                legendData.push(name);
                if (!$.isEmptyObject(value)) {
                    $.each(value, function (key1, value1) {
                        seriesData.push(value1);
                    });
                }

                series.push({
                    name: name,
                    type: 'line',
                    data: seriesData
                });
            });

            var option = {
                title: {
                    text: timeHtml,
                    textStyle: {
                        fontSize: 13
                    },
                    x: 'center'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    bottom: '0%',
                    data: legendData
                },
                yAxis: {},
                xAxis: {data: xAxis},
                series: series
            };
            myChart.setOption(option);
        } else {
            myChart.clear();
        }
    }

    //加载曲线图数据
    function loadLineData(dateType, lineId) {
        $.ajaxExt({
            url: '/lineDrvStandard/getLineAreaBadBhvTrendList.htm',
            data: {
                lineId: lineId,
                dateTy: dateType
            },
            success: function (data) {
                createLineChart(data);
            }
        })
    }

    function getDrvBadBhvCntSortList(dateType, lineId) {
        var $table = $('#table1');
        $('tr:gt(0)', $table).remove();
        $.ajaxExt({
            url: '/lineDrvStandard/getDrvBadBhvCntSortList.htm',
            data: {
                lineId: lineId,
                dateTy: dateType
            },
            success: function (data) {
                data = data || [];
                $.each(data, function (i, item) {

                    $table.append(['<tr class="good-tr">',
                        '<td class="good-num">' + item.rn + '</td>',
                        '<td>' + item.drvName + '</td>',
                        '<td>' + item.areaType1 || 0 + '</td>',
                        '<td>' + item.areaType2 || 0 + '</td>',
                        '<td>' + item.areaType3 || 0 + '</td>',
                        '<td>' + item.areaType4 || 0 + '</td>',
                        '<td class="c-green">' + item.freq || 0 + '</td>',
                        '</tr>'].join(''));
                });
            }
        })
    }

    $('#all-1').on('click', function () {
        getLineAreaBadBhvSort4BPage(1);
    });
    //黑点/全部排名/分页
    function getLineAreaBadBhvSort4BPage(rowNum) {
        var $pmModal = $('#table2'),
            $pmPage = $('#table2-page');
        var $table = $('table', $pmModal);
        $('tr:gt(0)', $table).remove();
        $.ajaxExt({
            url: '/lineDrvStandard/getLineAreaBadBhvSort4BPage.htm',
            loadMask: true,
            data: {
                page: rowNum,
                row: 10,
                dateTy: currentDateType,
                lineId: currentLineId
            },
            success: function (obj) {
                if (obj && obj.total) {
                    $(".site-body").removeClass("overflow-a");
                    $pmModal.show();
                    $pmPage.unbind().empty();
                    $.each(obj.data, function (i, item) {

                        $table.append([
                            '<tr>',
                            '<td>' + item.rn + '</td>',
                            '<td title="{0}">{0}</td>'.format(item.areaName || ''),
                            '<td title="{0}">{0}</td>'.format(item.lineDirection || ''),
                            '<td title="{0}">{0}</td>'.format(item.blackNature || ''),
                            '<td title="{0}">{0}</td>'.format(item.placeTypical || ''),
                            '<td>' + (item.loLgt || '') + '</td>',
                            '<td>' + (item.loLtt || '') + '</td>',
                            '<td>' + (item.radius || '' ) + '</td>',
                            '<td>' + (item.speedLimit || '' ) + '</td>',
                            '<td>' + (item.freq || '') + '</td>',
                            '</tr>'
                        ].join(''));
                    });
                    $pmPage.createPage({
                        pageCount: obj.pageTotal,
                        current: obj.rowBounds.pageNumber,
                        backFn: function (p) {
                            getLineAreaBadBhvSort4BPage(p);
                        }
                    });
                } else {
                    if (rowNum == 1) {
                        $.messager.alert("提示", '无数据');
                    }
                }
            }
        });
    }

    $('#all-2').on('click', function () {
        getLineAreaBadBhvSort4SPage(1);
    });
    //站点/全部排名/分页
    function getLineAreaBadBhvSort4SPage(rowNum) {
        var $pmModal = $('#table3'),
            $pmPage = $('#table3-page');
        var $table = $('table', $pmModal);
        $('tr:gt(0)', $table).remove();
        $.ajaxExt({
            url: '/lineDrvStandard/getLineAreaBadBhvSort4SPage.htm',
            loadMask: true,
            data: {
                page: rowNum,
                row: 10,
                dateTy: currentDateType,
                lineId: currentLineId
            },
            success: function (obj) {
                if (obj && obj.total) {
                    $(".site-body").removeClass("overflow-a");
                    $pmModal.show();
                    $pmPage.unbind().empty();
                    $.each(obj.data, function (i, item) {

                        $table.append([
                            '<tr>',
                            '<td>' + item.rn + '</td>',
                            '<td>' + (item.areaName || '') + '</td>',
                            '<td>' + (item.lineTy || '') + '</td>',
                            '<td>' + (item.stnTy || '') + '</td>',
                            '<td>' + (item.loLgt || '') + '</td>',
                            '<td>' + (item.loLtt || '') + '</td>',
                            '<td>' + (item.inRadius || '') + '</td>',
                            '<td>' + (item.outRadius || '') + '</td>',
                            '<td>' + (item.speedLimit || '') + '</td>',
                            '<td>' + (item.freq || '') + '</td>',
                            '</tr>'
                        ].join(''));
                    });
                    $pmPage.createPage({
                        pageCount: obj.pageTotal,
                        current: obj.rowBounds.pageNumber,
                        backFn: function (p) {
                            getLineAreaBadBhvSort4SPage(p);
                        }
                    });
                } else {
                    if (rowNum == 1) {
                        $.messager.alert("提示", '无数据');
                    }
                }
            }
        });
    }

    $('#all-3').on('click', function () {
        getLineAreaBadBhvSort4BSPage(1);
    });
    //站点区间/全部排名/分页
    function getLineAreaBadBhvSort4BSPage(rowNum) {
        var $pmModal = $('#table4'),
            $pmPage = $('#table4-page');
        var $table = $('table', $pmModal);
        $('tr:gt(0)', $table).remove();
        $.ajaxExt({
            url: '/lineDrvStandard/getLineAreaBadBhvSort4BSPage.htm',
            loadMask: true,
            data: {
                page: rowNum,
                row: 10,
                dateTy: currentDateType,
                lineId: currentLineId
            },
            success: function (obj) {
                if (obj && obj.total) {
                    $(".site-body").removeClass("overflow-a");
                    $pmModal.show();
                    $pmPage.unbind().empty();
                    $.each(obj.data, function (i, item) {

                        $table.append([
                            '<tr>',
                            '<td>' + item.rn + '</td>',
                            '<td>' + (item.areaName || '') + '</td>',
                            '<td>' + (item.lineDirection || '') + '</td>',
                            '<td>' + (item.freq || '') + '</td>',
                            '</tr>'
                        ].join(''));
                    });
                    $pmPage.createPage({
                        pageCount: obj.pageTotal,
                        current: obj.rowBounds.pageNumber,
                        backFn: function (p) {
                            getLineAreaBadBhvSort4BSPage(p);
                        }
                    });
                } else {
                    if (rowNum == 1) {
                        $.messager.alert("提示", '无数据');
                    }
                }
            }
        });
    }

    $('#all-4').on('click', function () {
        getDrvBadBhvCntSortPage(1);
    });
    //站点区间/全部排名/分页
    function getDrvBadBhvCntSortPage(rowNum) {
        var $pmModal = $('#table5'),
            $pmPage = $('#table5-page');
        var $table = $('table', $pmModal);
        $('tr:gt(0)', $table).remove();
        $.ajaxExt({
            url: '/lineDrvStandard/getDrvBadBhvCntSortPage.htm',
            loadMask: true,
            data: {
                page: rowNum,
                row: 10,
                dateTy: currentDateType,
                lineId: currentLineId
            },
            success: function (obj) {
                if (obj && obj.total) {
                    $(".site-body").removeClass("overflow-a");
                    $pmModal.show();
                    $pmPage.unbind().empty();
                    $.each(obj.data, function (i, item) {

                        $table.append([
                            '<tr>',
                            '<td>' + item.rn + '</td>',
                            '<td>' + item.drvName + '</td>',
                            '<td>' + item.areaType1 || 0 + '</td>',
                            '<td>' + item.areaType2 || 0 + '</td>',
                            '<td>' + item.areaType3 || 0 + '</td>',
                            '<td>' + item.areaType4 || 0 + '</td>',
                            '<td>' + item.freq || 0 + '</td>',
                            '</tr>'
                        ].join(''));
                    });
                    $pmPage.createPage({
                        pageCount: obj.pageTotal,
                        current: obj.rowBounds.pageNumber,
                        backFn: function (p) {
                            getDrvBadBhvCntSortPage(p);
                        }
                    });
                } else {
                    if (rowNum == 1) {
                        $.messager.alert("提示", '无数据');
                    }
                }
            }
        });
    }

    return {
        loadData: function (dateType, lineId) {
            if (currentDateType == dateType && currentLineId == lineId) {
                return;
            }
            currentDateType = dateType;
            currentLineId = lineId;

            loadBarData(dateType, lineId);
            //loadPieData(dateType, lineId);
            loadLineData(dateType, lineId);
            getDrvBadBhvCntSortList(dateType, lineId);
        }
    }
});

//不良行为地图模块
define('model3', ['gaodeMap', 'select2', 'template.min'], function () {
    var $filterArea = $('#filter-area'),//过滤条件区域
        $behaviorFbBtn = $('#behavior-fb-btn'),//地图行为分布按钮
        $timeFbBtn = $('#time-fb-btn'),//时间行为分布按钮
        $timeBehaviorDiv = $('#time-behavior-div'), //时间行为分布显示区域
        $iCenter = $('#iCenter'),//地图显示区域
        $area = $('#item3');//整个地图模块区域

    var currentLineId, currentDateType, currentLineNode;

    $filterArea.initForm();

    var $badBhvSelect = $('#badBhvSelect');
    $.ajaxExt({
        url: '/lineDrvStandard/getBadBhvList.htm',
        success: function (data) {

            data = data || [];
            var html = '';
            $.each(data, function (i, item) {
                html += '<option value="' + item.badBhvId + '">' + item.badBhvName + '</option>';
            });
            $badBhvSelect.append(html).select2();
        }
    })

    var gaodeMap = new MapGaoDe(116.39705658, 39.91710958, 4);


    $filterArea.on('change', function () {
        $('.tab2.active', $area).trigger('click', true);
    });

    $badBhvSelect.on('change', function () {
        $('.tab2.active', $area).trigger('click', true);
    });

    $area.on('click.mapChange', '.tab1', function (e, auto) {//线路区域不良行为地图 | 城市不良行为热力图 切换
        if ($(this).hasClass('active') && !auto) {
            return;
        }
        $('.tab1', $area).removeClass('active');
        $(this).addClass('active');


        if (this.id == 'behaviorMap-btn') {//线路区域不良行为地图
            $filterArea.show();
            $('.tab2.active', $area).trigger('click', true);
        } else {//城市不良行为热力图
            $filterArea.hide();
            $behaviorFbBtn.addClass('active');
            $timeFbBtn.removeClass('active');
            $timeBehaviorDiv.hide();
            gaodeMap.map.clearMap();
            loadLine();
            $iCenter.show();
            getCityBadBhvTherDiagData();
        }
    }).on('click.typeChange', '.tab2', function (e, auto) {//地图行为分布 时间行为分布 切换
        if ($(this).hasClass('active') && !auto) {
            return;
        }
        $('.tab2', $area).removeClass('active');
        $(this).addClass('active');

        if (this.id == 'behavior-fb-btn') {//地图行为分布
            gaodeMap.map.clearMap();
            heatmap && heatmap.setDataSet({
                data: []
            });
            $timeBehaviorDiv.hide();
            $iCenter.show();
            loadLine();
            getBadBhvMapData();
        } else {//时间行为分布
            $iCenter.hide();
            $timeBehaviorDiv.show();
            getBadBhvTimeDist();
        }
    });


    //获取地图行为分布数据
    function getBadBhvMapData() {
        $.ajaxExt({
            url: '/lineDrvStandard/getBadBhvMapData.htm',
            loadMask: {
                target: $area
            },
            data: $.extend({
                lineId: currentLineId,
                dateTy: currentDateType
            }, $filterArea.getValues()),
            success: function (data) {
                data = data || [];
                /**
                 * 标记点击显示信息
                 * @param e 目标元素
                 */
                function markerClick(e) {
                    var infoWindow = new AMap.InfoWindow({
                        content: e.target.content
                    });
                    infoWindow.open(gaodeMap.map, e.target.position);
                }

                $.each(data, function (i, item) {

                    var circle = new AMap.Circle({
                        center: new AMap.LngLat(item.loLgt, item.loLtt),// 圆心位置
                        //center: new AMap.LngLat(113.281774, 23.133718),
                        //radius: 200, //半径
                        radius: (11 - item.rn ? 11 - item.rn : 0) * 10 + 100,
                        zIndex: 150,
                        strokeColor: "#F33", //线颜色
                        strokeOpacity: 1, //线透明度
                        strokeWeight: 3, //线粗细度
                        fillColor: "#ee2200", //填充颜色
                        fillOpacity: 0.35//填充透明度
                    });
                    circle.content = template('temp', item);
                    circle.position = [item.loLgt, item.loLtt];
                    //circle.position = [113.281774, 23.133718];
                    circle.on('mouseover', markerClick);
                    circle.emit('click', {target: circle});
                    circle.setMap(gaodeMap.map);

                });
            }
        })
    }

    //获取时间行为分布数据
    function getBadBhvTimeDist() {
        $.ajaxExt({
            url: '/lineDrvStandard/getBadBhvTimeDist.htm',
            loadMask: {
                target: $area
            },
            data: $.extend({
                lineId: currentLineId,
                dateTy: currentDateType,
                badBhvId: $badBhvSelect.val()
            }, $filterArea.getValues()),
            success: function (data) {

                var myChart = echarts.init(document.getElementById('chart10'));
                if (data) {

                    var xAxis = [],
                        seriesData = [];
                    $.each(data, function (key, value) {
                        xAxis.push(key.replace('hour', ''));
                        seriesData.push(value);
                    });

                    var option = {
                        title: {
                            text: '区域不良行为数量小时统计',
                            x: 'center'
                        },
                        tooltip: {
                            trigger: 'axis'
                        },
                        xAxis: {data: xAxis, boundaryGap: true},
                        yAxis: {
                            axisLine: {
                                show: false
                            },
                            axisTick: {
                                show: false
                            }
                        },
                        series: [{
                            type: 'bar',
                            name: '数量',
                            data: seriesData
                        }]
                    };
                    myChart.setOption(option);
                } else {
                    myChart.clear();
                }


            }
        })
    }

    var heatmap;
    gaodeMap.map.plugin(["AMap.Heatmap"], function () {

        heatmap = new AMap.Heatmap(gaodeMap.map, {
            radius: 25, //给定半径
            opacity: [0, 0.8]
        })
    });
    //加载热力图数据
    function getCityBadBhvTherDiagData() {
        $.ajaxExt({
            url: '/lineDrvStandard/getCityBadBhvTherDiagData.htm',
            loadMask: {
                target: $area
            },
            data: {
                lineId: currentLineId,
                dateTy: currentDateType
            },
            success: function (data) {
                data = data || [];
                heatmap.setDataSet({
                    data: data,
                    max: 5
                });
            }
        })
    }

    //查找线路地图信息及线路车辆信息
    function loadLine(city) {
        var treeNode = currentLineNode;
        var lineName = treeNode.name;
        if (treeNode.province == '北京' || treeNode.province == '上海' ||
            treeNode.province == '天津' || treeNode.province == '重庆') {
            city = treeNode.province;
        } else {
            city = treeNode.city;
        }
        //gaodeMap.setMapCity(city);
        AMap.service(["AMap.LineSearch"], function () {
            var linesearch = new AMap.LineSearch({
                pageIndex: 1,
                city: city,
                pageSize: 1,
                extensions: 'all'
            });
            linesearch.search(lineName, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    var lineNum = result.lineInfo.length;
                    if (lineNum) {
                        var lineArr = result.lineInfo;
                        var pathArr = lineArr[0].path;
                        var stops = lineArr[0].via_stops;
                        var startPot = stops[0].location;
                        var endPot = stops[stops.length - 1].location;
                        var stationsLen = lineArr[0].via_stops.length;
                        for (var i = 0; i < stationsLen; i++) {
                            new AMap.Marker({
                                map: gaodeMap.map,
                                position: lineArr[0].via_stops[i].location,
                                offset: new AMap.Pixel(-3, -3),
                                icon: $.getProjectName() + "/component/map/images/way_btn2.png",
                                title: lineArr[0].via_stops[i].name,
                                zIndex: 13
                            });
                        }
                        drawbusLine(startPot, endPot, pathArr);
                    }
                } else {
                    $.messager.alert('提示', '未找到对应的线路');
                }
            });
        });
    }

    //绘制路线
    function drawbusLine(startPot, endPot, BusArr) {
        //绘制起点，终点
        new AMap.Marker({
            map: gaodeMap.map,
            position: [startPot.lng, startPot.lat], //基点位置
            icon: $.getProjectName() + "/component/map/images/start_flag.png",
            zIndex: 13
        });
        new AMap.Marker({
            map: gaodeMap.map,
            position: [endPot.lng, endPot.lat], //基点位置
            icon: $.getProjectName() + "/component/map/images/end_flag.png",
            zIndex: 13
        });
        //绘制乘车的路线
        new AMap.Polyline({
            map: gaodeMap.map,
            path: BusArr,
            strokeColor: "#09f",//线颜色
            strokeOpacity: 0.8,//线透明度
            strokeWeight: 6//线宽
        });
        gaodeMap.mapFitView();
        gaodeMap.setMapLevel(12);
    }

    return {
        loadData: function (dateType, lineId, lineNode) {
            if (currentDateType == dateType && currentLineId == lineId) {
                return;
            }

            if (lineNode) {
                currentLineNode = lineNode;
            }

            currentDateType = dateType;
            currentLineId = lineId;
            $('.tab1.active', $area).trigger('click', true);
        }
    }
});

require(['echarts3', 'lineTree', 'model1', 'model3'], function (echarts, lineTree, model1, model3) {
    window.echarts = echarts;
    lineTree.init(function (lineId, dateType, lineNode) {
        //绑定树节点点击事件
        $.setCurrentLine(lineId);
        model1.loadData(dateType, lineId);
        model3.loadData(dateType, lineId, lineNode);
    });
});
